package org.black.phoneUtility.sms.reply.widget;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.black.phoneUtility.R;
import org.black.phoneUtility.sms.reply.database.dataModel.ReplyToSpecificNumber;
import org.black.phoneUtility.sms.reply.database.dataModel.ReplyToSpecificNumberFromContact;
import org.black.phoneUtility.sms.reply.database.dataModel.ReplyToSpecificNumberFromNumbersFromContactGroup;
import org.black.phoneUtility.sms.reply.database.dataModel.ReplyToSpecificNumberFromUserInput;

import android.content.Context;
import android.telephony.PhoneNumberUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ReplyToSpecificNumberFromAdapter extends BaseAdapter {

	final static class ViewHolder {
		RelativeLayout singleRowRelativelayout;
		TextView singleRowTitle;
		RelativeLayout doubleRowRelativeLayout;
		TextView doubleRowTitle;
		TextView doubleRowSummary;
	}

	private List<ReplyToSpecificNumber> replyToSpecificNumbers;
	private Map<Integer, Boolean> selectionItems;
	private Context context;
	private LayoutInflater layoutInflater;

	public ReplyToSpecificNumberFromAdapter(Context context) {
		this.context = context;
		this.layoutInflater = LayoutInflater.from(this.context);
		this.replyToSpecificNumbers = new ArrayList<ReplyToSpecificNumber>();
		this.selectionItems = new HashMap<Integer, Boolean>();
	}

	public ReplyToSpecificNumberFromAdapter(Context context,
			List<ReplyToSpecificNumber> replyToSpecificNumbers) {
		this(context);
		this.replyToSpecificNumbers = replyToSpecificNumbers;
	}

	public void setReplyToSpecificNumbers(
			List<ReplyToSpecificNumber> replyToSpecificNumbers) {
		this.replyToSpecificNumbers = replyToSpecificNumbers;
	}

	public boolean add(ReplyToSpecificNumber replyToSpecificNumber) {
		if (this.replyToSpecificNumbers == null) {
			this.replyToSpecificNumbers = new ArrayList<ReplyToSpecificNumber>();
		}
		boolean result = this.replyToSpecificNumbers.add(replyToSpecificNumber);
		this.notifyDataSetChanged();
		return result;

	}

	public boolean add(List<ReplyToSpecificNumber> replyToSpecificNumbers) {
		boolean result = false;
		if (replyToSpecificNumbers != null
				&& this.replyToSpecificNumbers != null) {
			result = this.replyToSpecificNumbers.addAll(replyToSpecificNumbers);
			this.notifyDataSetChanged();
		}
		return result;
	}

	public boolean remove(ReplyToSpecificNumber replyToSpecificNumber) {
		boolean result = false;
		if (this.replyToSpecificNumbers != null) {
			result = this.replyToSpecificNumbers.remove(replyToSpecificNumber);
			this.notifyDataSetChanged();
		}
		return result;
	}

	public boolean remove(List<ReplyToSpecificNumber> replyToSpecificNumbers) {
		boolean result = false;
		if (replyToSpecificNumbers != null
				&& this.replyToSpecificNumbers != null) {
			result = this.replyToSpecificNumbers
					.removeAll(replyToSpecificNumbers);
			this.notifyDataSetChanged();
		}
		return result;
	}

	public void addSelection(int position, boolean checked) {
		this.selectionItems.put(position, checked);
		this.notifyDataSetChanged();
	}

	public void removeSelection(int position) {
		this.selectionItems.remove(position);
		this.notifyDataSetChanged();
	}

	public boolean removeSelections() {
		boolean result = false;
		List<ReplyToSpecificNumber> replyToSpecificNumbers = new ArrayList<ReplyToSpecificNumber>();
		Set<Integer> keys = this.selectionItems.keySet();
		for (Integer key : keys) {
			boolean checked = this.selectionItems.get(key);
			if (checked == true) {
				replyToSpecificNumbers
						.add(this.replyToSpecificNumbers.get(key));
			}
		}
		result = this.replyToSpecificNumbers.removeAll(replyToSpecificNumbers);
		this.notifyDataSetChanged();
		return result;
	}

	public void clearSelection() {
		this.selectionItems.clear();
		this.notifyDataSetChanged();
	}

	public boolean isPositionChecked(int position) {
		Boolean result = this.selectionItems.get(position);
		return result == null ? false : result;
	}

	@Override
	public int getCount() {
		if (this.replyToSpecificNumbers == null) {
			return 0;
		}

		return this.replyToSpecificNumbers.size();
	}

	@Override
	public Object getItem(int position) {
		if (this.replyToSpecificNumbers == null) {
			return null;
		}

		return this.replyToSpecificNumbers.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		ReplyToSpecificNumber replyToSpecificNumber = this.replyToSpecificNumbers
				.get(position);
		if (convertView == null) {
			convertView = this.layoutInflater.inflate(
					R.layout.sms_reply_to_specific_number_from_row, null);
			viewHolder = new ViewHolder();
			viewHolder.singleRowRelativelayout = (RelativeLayout) convertView
					.findViewById(R.id.smsReplyToSpecificNumberFromRowNumberInformationSingleRowPanelRelativeLayout);
			viewHolder.singleRowTitle = (TextView) convertView
					.findViewById(R.id.smsReplyToSpecificNumberFromRowNumberInformationSingleRowTextView);
			viewHolder.doubleRowRelativeLayout = (RelativeLayout) convertView
					.findViewById(R.id.smsReplyToSpecificNumberFromRowNumberInformationTwoRowsPanelRelativeLayout);
			viewHolder.doubleRowTitle = (TextView) convertView
					.findViewById(R.id.smsReplyToSpecificNumberFromRowNumberInformationTowRowsTitleTextView);
			viewHolder.doubleRowSummary = (TextView) convertView
					.findViewById(R.id.smsReplyToSpecificNumberFromRowNumberInformationTowRowsDetailTextView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if (replyToSpecificNumber instanceof ReplyToSpecificNumberFromUserInput) {
			viewHolder.singleRowRelativelayout.setVisibility(View.VISIBLE);
			viewHolder.doubleRowRelativeLayout.setVisibility(View.GONE);

			String title = null;
			ReplyToSpecificNumberFromUserInput replyToSpecificNumberFromUserInput = (ReplyToSpecificNumberFromUserInput) replyToSpecificNumber;
			title = PhoneNumberUtils
					.formatNumber(replyToSpecificNumberFromUserInput
							.getPhoneNumber());

			viewHolder.singleRowTitle.setText(title);
		} else {
			viewHolder.singleRowRelativelayout.setVisibility(View.GONE);
			viewHolder.doubleRowRelativeLayout.setVisibility(View.VISIBLE);
			if (replyToSpecificNumber instanceof ReplyToSpecificNumberFromContact) {
				ReplyToSpecificNumberFromContact replyToSpecificNumberFromContact = (ReplyToSpecificNumberFromContact) replyToSpecificNumber;
				viewHolder.doubleRowTitle
						.setText(replyToSpecificNumberFromContact.getName());
				viewHolder.doubleRowSummary.setText(PhoneNumberUtils
						.formatNumber(replyToSpecificNumberFromContact
								.getPhoneNumber()));
			} else {
				ReplyToSpecificNumberFromNumbersFromContactGroup replyToSpecificNumberFromNumbersFromContactGroup = (ReplyToSpecificNumberFromNumbersFromContactGroup) replyToSpecificNumber;
				viewHolder.doubleRowTitle
						.setText(replyToSpecificNumberFromNumbersFromContactGroup.getName()
								+ " ( "
								+ replyToSpecificNumberFromNumbersFromContactGroup
										.getGroupName() + " ) ");
				viewHolder.doubleRowSummary.setText(PhoneNumberUtils
						.formatNumber(replyToSpecificNumberFromNumbersFromContactGroup
								.getPhoneNumber()));
			}

		}
		if (this.selectionItems.get(position) != null) {
			convertView.setBackgroundColor(this.context.getResources()
					.getColor(android.R.color.holo_blue_light));
		} else {
			convertView.setBackground(this.context.getResources().getDrawable(
					android.R.drawable.list_selector_background));
		}
		return convertView;
	}

}
